﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsForms;

using System.Data.SqlClient;

namespace WindowsFormsApp1
{
    public partial class Saichengtianjai : Form
    {
        DBHelper db = new DBHelper();
        DataSet ds = new DataSet();
        public Saichengtianjai()
        {
            InitializeComponent();
        }

        private void Saichengtianjai_Load(object sender, EventArgs e)
        {
            db.OpenConnection();
            string str = "select * from TeamInfo";
            SqlDataAdapter dataAdapter = new SqlDataAdapter(str,db.Connection);

            dataAdapter.Fill(ds, "TeamInfo");
            
            //和下拉列表关联数据
            this.comboBox1.DataSource = ds.Tables["TeamInfo"].Copy();
            //绑定实际值
            this.comboBox1.ValueMember = "id";
            //绑定显示的值
            this.comboBox1.DisplayMember = "teamName";

            //解决两个combox适用同一个数据源联动
            this.comboBox2.DataSource = ds.Tables["TeamInfo"].Copy();
            //绑定实际值
            this.comboBox2.ValueMember = "id";
            //绑定显示的值
            this.comboBox2.DisplayMember = "teamName";


            db.CloseConnection();

        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (this.textBox1.Text.Trim().Equals(string.Empty) || this.textBox2.Text.Trim().Equals(string.Empty)
                || this.textBox3.Text.Trim().Equals(string.Empty) || this.textBox4.Text.Trim().Equals(string.Empty)
                || this.textBox5.Text.Trim().Equals(string.Empty) ) {

                MessageBox.Show("请将信息输入完整~","错误提示框",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
               }

            if (this.comboBox1.Text == this.comboBox2.Text)
            {
                MessageBox.Show("主队和客队不能是同一个队伍！~", "错误提示框", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            db.OpenConnection();

            string str= "insert into ScheduleInfo(count,hostTeamId,awayTeamId,competitionTime,competitionPosition,score,entryPerson,entryTime) values" +
                "(N'"+this.textBox1.Text+"',N'"+this.comboBox1.SelectedValue + "',N'"+ this.comboBox2.SelectedValue + "',N'"+this.textBox2.Text+ "',N'" + this.textBox4.Text + "',N'" + this.textBox3.Text + "',N'" + this.textBox5.Text + "',N'"+ DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + "')";

            SqlCommand comm = new SqlCommand(str,db.Connection);
            int iret = comm.ExecuteNonQuery();

            if (iret > 0)
            {
                MessageBox.Show("添加成功！");
            }
            else {
                MessageBox.Show("添加失败！");
            }
            db.CloseConnection();

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = null;
            this.textBox2.Text = null;
            this.textBox3.Text = null;
            this.textBox4.Text = null;
            this.textBox5.Text = null;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
